package com.sumainfor.zuul.controller;

import com.sumainfor.common.annotation.SysLog;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.R;
import com.sumainfor.zuul.dao.SysUserDao;
import com.sumainfor.zuul.entity.SysUserEntity;
import com.sumainfor.zuul.form.PasswordForm;
import com.sumainfor.zuul.service.SysUserRoleService;
import com.sumainfor.zuul.service.SysUserService;
import com.sumainfor.zuul.util.PageUtils;
import com.sumainfor.zuul.validator.Assert;
import com.sumainfor.zuul.validator.ValidatorUtils;
import com.sumainfor.zuul.validator.group.AddGroup;
import com.sumainfor.zuul.validator.group.UpdateGroup;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    SysUserDao sysUserDao;


    /**
     * 所有用户列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysUserService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info")
    public R info() {
        return R.ok().put("user", getUser());
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改密码")
    @PostMapping("/password")
    public R password(@RequestBody PasswordForm form) {
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");
        System.out.println("未加密前的密码");
        System.out.println("PasswordForm"+form);
        System.out.println("form.getPassword()"+form.getPassword());
        System.out.println("form.getNewPassword()"+form.getNewPassword());
        System.out.println(" getUser().getSalt()"+ getUser().getSalt());

        Integer user_id = getUserId();
        Map<String,Object> SaltMap=sysUserDao.getUserSalt(String.valueOf(user_id));

        //sha256加密
        //String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
        String password = new Sha256Hash(form.getPassword(), SaltMap.get("salt").toString()).toHex();
        //sha256加密
       // String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();
        String newPassword = new Sha256Hash(form.getNewPassword(), SaltMap.get("salt").toString()).toHex();

        System.out.println("1111111111111111111");
        System.out.println("password-----------"+password);
        System.out.println("newPassword-----------"+newPassword);
        System.out.println("getUserId()---------"+getUserId());


        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return R.error("原密码不正确");
        }

        return R.ok();
    }

//    public static void main(String[] args) {
//        //9ec9750e709431dad22365cabc5c625482e574c74adaebba7dd02f1129e4ce1d
//        String password = new Sha256Hash("admin","YzcmCZNvbXocrsz9dm8e").toHex();
//        String Newpassword = new Sha256Hash("tc-2020","YzcmCZNvbXocrsz9dm8e").toHex();
//        System.out.println(Newpassword);
//        System.out.println(password);
//
//    }


    /**
     * 用户信息
     */
    @GetMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("userId") Integer userId) {
        if (userId == null) {
            return R.error("参数不完整");
        }
        SysUserEntity user = sysUserService.getUserEntityById(userId);
        //获取用户所属的角色列表
        List<Integer> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @PostMapping("/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);

        sysUserService.save(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @PostMapping("/update")
    @RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return R.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return R.error("当前用户不能删除");
        }

        sysUserService.deleteBatch(userIds);

        return R.ok();
    }

    /**
     * 获得EPC 工程部所有的用户id 和名称
     *
     * @return
     */
    @RequestMapping("/getEpcUsersData")
    public JsonResult getEpcUsersData() {
        return sysUserService.getEpcUsersData();
    }

    /**
     * 功能描述: 
     * 获取所有维修师傅列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/9 17:23
     */
    @GetMapping("/getRepiarUserList")
    public JsonResult getRepiarUserList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        return result.put(sysUserService.getRepiarUserList(params));
    }

    /**
     * 功能描述: 
     * 根据用户编号解除当前用户微信绑定信息
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/8 16:10
     */
    @PostMapping("/UnbindUser")
    public JsonResult UnbindUser(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(sysUserService.UnbindUser(params)){
            result.putSuccess("解除绑定成功！");
        }else{
            result.putFailed("服务器繁忙,请稍后重试！");
        }
        return result;
    }

}
